package cn.libiting.dateRange.utils;

import java.util.Calendar;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: wangxc
 * Date: 12-9-24
 * Time: 上午11:56
 * To change this template use File | Settings | File Templates.
 */
public class DateUtil {

    public static Date getLastDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar
                .getActualMaximum(Calendar.DAY_OF_MONTH));

        endOfDay(calendar);
        return calendar.getTime();
    }


    /**
     * 得到本月的第一天
     * @return
     */
    public static Date getFirstDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar
                .getActualMinimum(Calendar.DAY_OF_MONTH));

        beginningOfDay(calendar);

        return calendar.getTime();
    }


    /**
     * 前一月同一天
     */
    public static Date lastMonthSameDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        calendar.add(Calendar.MONTH, -1);
        return calendar.getTime();
    }


    /**
     * 前一周同一天
     * @param date
     * @return
     */
    public static Date lastWeekSameDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        calendar.add(Calendar.WEEK_OF_YEAR, -1); //TODO, 没有单独的WEEK， 有两个相关的： 一个是WEEK_OF_YEAR， 另一个是WEEK_OF_MONTH
        return calendar.getTime();
    }


    /**
     * 当前周的第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDateOfWeek(Date date) {
        Calendar startDate = Calendar.getInstance();
        startDate.setTime(date);

        int dayOfWeek = startDate.get(Calendar.DAY_OF_WEEK);
        while (dayOfWeek  != Calendar.MONDAY) {
            startDate.add(Calendar.DATE, -1);
            dayOfWeek = startDate.get(Calendar.DAY_OF_WEEK);
        }

        beginningOfDay(startDate);

        return startDate.getTime();
    }

    /**
     * 当前周的最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDateOfWeek(Date date) {
        Calendar endDate = Calendar.getInstance();
        endDate.setTime(date);

        int dayOfWeek = endDate.get(Calendar.DAY_OF_WEEK);
        while (dayOfWeek  != Calendar.SUNDAY) {
            endDate.add(Calendar.DATE, 1);
            dayOfWeek = endDate.get(Calendar.DAY_OF_WEEK);
        }

        endOfDay(endDate);

        return endDate.getTime();
    }

    public static void endOfDay(final Calendar c) {
        c.set(Calendar.HOUR_OF_DAY,23);
        c.set(Calendar.MINUTE,59);
        c.set(Calendar.SECOND,59);
        c.set(Calendar.MILLISECOND,999);
    }


    public static void beginningOfDay(final Calendar c) {
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
    }

}
